﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace Sparkle.UI
{
    public class GroupPanel : StackPanel, IDisposable
    {
        public static int HeaderHeight = 30;

        public static int HeadCornerRadius = 7;

        private Column _columns = Column.Double;

        public Column Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }


        private string _title = string.Empty;
        public string Title { get { return _title; } set { _title = value; } }

        Grid grid = new Grid();

        public GroupPanel()
        {
            this.Loaded += new RoutedEventHandler(GroupPanel_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(GroupPanel_SizeChanged);
        }

        void GroupPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            foreach (var child in this.Children)
            {
                if ((child is MetaControl))
                {
                    MetaControl metaControl = (child as MetaControl);
                    if (metaControl.IsFullWidth)
                    {
                        metaControl.Width = this.Width;
                    }
                }
            }
        }

        public void GroupPanel_Loaded(object sender, RoutedEventArgs e)
        {
            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(HeaderHeight, GridUnitType.Pixel) });
            //grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Star)});

            switch (_columns)
            {
                case Column.Single:
                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Star) });
                    break;
                case Column.Triple:
                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Star) });
                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Star) });
                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Star) });
                    break;
                case Column.Double:
                default:
                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(200, GridUnitType.Star) });
                    grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(200, GridUnitType.Star) });
                    break;
            }

            UIElement[] copyChildren = new UIElement[this.Children.Count];
            this.Children.CopyTo(copyChildren, 0);

            this.Children.Clear();

            Border border = CreateGroupPanel(this.Title);
            border.SetValue(Grid.RowProperty, 0);
            border.SetValue(Grid.ColumnProperty, 0);

            //this.Children.Add(border);

            StackPanel mainStkPnl = new StackPanel()
            {
                  Orientation= Orientation.Vertical
            };

            mainStkPnl.Children.Add(border);
            mainStkPnl.Children.Add(grid);

            Border mainBorder = new Border()
            {
                Child = mainStkPnl,
                Margin = new Thickness(1),
                CornerRadius = new CornerRadius(4),
                BorderBrush = new SolidColorBrush(Colors.White),
                BorderThickness= new Thickness(1)
            };

            this.Children.Add(mainBorder);
            setControlPosition(copyChildren);
        }

        private void setControlPosition(UIElement[] copyChildren)
        {
            int count = 0;
            int gridRow = 0;
            int gridCol = 0;

            foreach (var control in copyChildren)
            {
                if (control is MetaControl)
                {
                    MetaControl tmpMetaControl = (control as MetaControl);

                    switch (_columns)
                    {
                        case Column.Single:
                            //gridCol = 0;// by default is 0;
                            gridRow++;
                            break;
                        case Column.Triple:
                            gridCol = count % 3;
                            break;
                        case Column.Double:
                        default:
                            gridCol = count % 2;
                            break;
                    }
                    if (gridCol == 0 || tmpMetaControl.IsFullWidth)
                    {
                        gridRow++;
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Star) });
                    }

                    if (tmpMetaControl.IsFullWidth)
                    {
                        control.SetValue(Grid.RowProperty, gridRow - 1);
                        control.SetValue(Grid.ColumnProperty, 0);

                        switch (_columns)
                        {
                            case Column.Single:
                                break;
                            case Column.Triple:
                                count = gridRow * 3;
                                control.SetValue(Grid.ColumnSpanProperty, 3);
                                break;
                            case Column.Double:
                            default:
                                count = gridRow * 2;
                                control.SetValue(Grid.ColumnSpanProperty, 2);
                                break;
                        }

                    }
                    else
                    {
                        control.SetValue(Grid.RowProperty, gridRow - 1);
                        control.SetValue(Grid.ColumnProperty, gridCol);

                        count++;
                    }

                    this.grid.Children.Add(control);
                }
            }
        }

        private Border CreateGroupPanel(string groupName)
        {
            Border grpHeaderPnl = new Border()
            {
                Margin = UIDefaults.GetMargin(),
                CornerRadius = new CornerRadius(4),
                HorizontalAlignment = HorizontalAlignment.Left,
                Width = this.Width - (UIDefaults.Margin * 4),
                Background = new SolidColorBrush()
                {
                    Color = Colors.Blue
                },
                Child = new TextBlock()
                {
                    Text = groupName,
                    Foreground = new SolidColorBrush() { Color = Colors.White }
                }
            };

            return grpHeaderPnl;
        }


        #region DependencyProperty
        public static readonly DependencyProperty ControlsProperty
          = DependencyProperty.Register(
            "Templates",
            typeof(Collection<MetaControl>),
            typeof(GroupPanel),
            new PropertyMetadata(new Collection<MetaControl>())
          );

        public Collection<MetaControl> Templates
        {
            get
            {
                return (Collection<MetaControl>)GetValue(ControlsProperty);
            }
        }

        /*
         * 
         * <local:DataPanel> 
         *   <local:DataPanel.Controls> 
         *       <Button Content="A"/> 
         *       <Button Content="B"/> 
         *   </local:DataPanel.Controls> 
         *</local:DataPanel> 
         */

        #endregion


        #region IDisposable Members

        public void Dispose()
        {
            this.grid.Children.Clear();
            this.Children.Clear();
        }

        #endregion
    }

}
